package com.fish;
import com.math.Vector;
/**
* @author Dror
*
* email: gumjum.o.o@gmail.com
*
*/
public class Route {
Fish fish;
boolean busy = false;
public static final int FORWARD_STATE_NUM = 30;
public static final int BACKWARD_STATE_NUM = 10;
// public State tmpStates[];
public State [] forward;
public State [] backward;
Vector target;
// Vector saved_p[];
// Vector saved_r[];
State tmpState;
public Route(Fish fish2) {
this.fish = fish2;
target = new Vector();
// tmpStates = new State[FORWARD_STATE_NUM];
// for(int i = 0;i<FORWARD_STATE_NUM;i++)
// tmpStates[i] = new State(fish2);
forward = new State[FORWARD_STATE_NUM];
backward = new State[BACKWARD_STATE_NUM];
for(int i = 0;i<FORWARD_STATE_NUM;i++)
forward[i] = new State(fish2);
for(int i = 0;i<BACKWARD_STATE_NUM;i++)
backward[i] = new State(fish2);
}
public void setTarget(Vector target){
this.target.set(target);
for(State s:forward){
s.target.set(target);
}
}
public void calcNewRoute(){
calcNewRoute(1);
}
public void calcNewRoute(int in){
for(int i = in;i<FORWARD_STATE_NUM;i++){
forward[i-1].resetTries();
predictNew(i,i-1);
// System.out.println("-1-" + i);
}
}
public void stepForward(){
for(int i = BACKWARD_STATE_NUM-1;i>0;i--){
backward[i].set(backward[i-1]);
}
backward[0].set(forward[0]);
for(int i = 0;i<FORWARD_STATE_NUM-1;i++)
forward[i].set(forward[i+1]);
if (!checkCurRoute()){
calcNewRoute();
forward[FORWARD_STATE_NUM-2].resetTries();
predictNew(FORWARD_STATE_NUM-1,FORWARD_STATE_NUM-2);
}else{
forward[FORWARD_STATE_NUM-2].resetTries();
predictNew(FORWARD_STATE_NUM-1,FORWARD_STATE_NUM-1);
}
}
public int predictNew(int pr, int src){
float pre = -1;
int to = src;
State s = new State(fish);
pre = forward[src].predict(s);
while(pre == -1){
if (to > 1)
to = predictNew(to,to-1);
else{
pre = forward[src].predict(s,1);
if(pre == -1)
pre = forward[src].fpredict(s,1);
}
}
forward[pr].set(s);
checkTarget(pr);
return to;
}
public void predictNew(int pr){
predictNew(pr,pr);
}
public boolean checkCurRoute(){
for(State s: forward)
if(!World.checkFishPos(s.p, fish))
return false;
return true;
}
private void checkTarget(int index) {
tmpState = forward[index];
float d = tmpState.target.sub(tmpState.p).length();
if(d<3){
fish.setTarget(forward[index].p);
tmpState.setTarget(fish.target);
for(int i = index;i<FORWARD_STATE_NUM;i++){
forward[i].setTarget(fish.target);
}
}
}
public void stepBackward(){
}
}